package com.ruoyi.project.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.domain.Xyzlb;
import com.ruoyi.project.mapper.XyzlbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.LectureMapper;
import com.ruoyi.project.domain.Lecture;
import com.ruoyi.project.service.ILectureService;
import com.ruoyi.common.support.Convert;

/**
 * 授课记录 服务层实现
 * 
 * @author ruoyi
 * @date 2018-11-02
 */
@Service
public class LectureServiceImpl implements ILectureService 
{
	@Autowired
	private LectureMapper lectureMapper;

	@Autowired
	private XyzlbMapper xyzlbMapper;

	/**
     * 查询授课记录信息
     * 
     * @param lectureId 授课记录ID
     * @return 授课记录信息
     */
    @Override
	public Lecture selectLectureById(Long lectureId)
	{
	    return lectureMapper.selectLectureById(lectureId);
	}
	
	/**
     * 查询授课记录列表
     * 
     * @param lecture 授课记录信息
     * @return 授课记录集合
     */
	@Override
	public List<Lecture> selectLectureList(Lecture lecture)
	{
	    return lectureMapper.selectLectureList(lecture);
	}
	
    /**
     * 新增授课记录
     * 
     * @param lecture 授课记录信息
     * @return 结果
     */
	@Override
	public int insertLecture(Lecture lecture)
	{
		Lecture temp = new Lecture();
		temp.setUserId(lecture.getUserId());

		int studentId= Math.toIntExact(lecture.getUserId());
		if (lectureMapper.selectLectureList(temp).size() == 0){
			Xyzlb xyzlb=xyzlbMapper.selectXyzlbById(studentId);
			if (xyzlb.getCourseType().equals("level")){
				//分级课程失效日期
				Calendar yue = Calendar.getInstance();
				yue.set(Calendar.MONTH,yue.get(Calendar.MONTH)+6);
				Date date1=yue.getTime();
				xyzlb.setExpirationDate(date1);
				xyzlbMapper.updateXyzlb(xyzlb);
			}
			else {
				//年度课程失效日期
				Calendar curr = Calendar.getInstance();
				curr.set(Calendar.YEAR,curr.get(Calendar.YEAR)+1);
				Date date1=curr.getTime();
				xyzlb=xyzlbMapper.selectXyzlbById(studentId);
				xyzlb.setExpirationDate(date1);
				xyzlbMapper.updateXyzlb(xyzlb);
			}
		}
		return lectureMapper.insertLecture(lecture);
	}
	
	/**
     * 修改授课记录
     * 
     * @param lecture 授课记录信息
     * @return 结果
     */
	@Override
	public int updateLecture(Lecture lecture)
	{
	    return lectureMapper.updateLecture(lecture);
	}

	/**
     * 删除授课记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLectureByIds(String ids)
	{
		return lectureMapper.deleteLectureByIds(Convert.toStrArray(ids));
	}

	/**
	 * 根据日期查询授课信息
	 * @param classtime
	 * @return
	 */
	@Override
	public List<Lecture> selectLectureclasstime(Date classtime){
		Lecture lecture=new Lecture();
		lecture.setClasstime(classtime);
		return lectureMapper.selectLectureList(lecture);
	}

	/**
	 * 根据歌曲名查询授课记录
	 * @param songName
	 * @return
	 */
	@Override
	public List<Lecture> selectLecturesongName(String songName){
		Lecture lecture=new Lecture();
		lecture.setSongName(songName);
		return lectureMapper.selectLectureList(lecture);
	}

	/**
	 * 根据部门id查询授课记录
	 * @param deptId
	 * @return
	 */
	@Override
	public List<Lecture> selectLecturedeptId(long deptId){
		Lecture lecture=new Lecture();
		lecture.setDeptId(deptId);
		return lectureMapper.selectLectureList(lecture);
	}

	/**
	 * 根据部门名称查询授课信息
	 * @param deptName
	 * @return
	 */
	@Override
	public List<Lecture> selectLecturedeptName(String deptName){
		Lecture lecture=new Lecture();
		lecture.setDeptName(deptName);
		return lectureMapper.selectLectureList(lecture);
	}

	/**
	 * 根据教师id查询授课信息
	 * @param teacherId
	 * @return
	 */
	@Override
	public List<Lecture> selectLectureteacherId(long teacherId){
		Lecture lecture=new Lecture();
		lecture.setTeacherId(teacherId);
		return lectureMapper.selectLectureList(lecture);
	}

	/**
	 * 根据教师姓名查询授课信息
	 * @param teacherName
	 * @return
	 */
	@Override
	public List<Lecture> selectLectureteacherName(String teacherName){
		Lecture lecture=new Lecture();
		lecture.setTeacherName(teacherName);
		return lectureMapper.selectLectureList(lecture);
	}


}
